package com.zlzkj.app.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zlzkj.app.model.Admin;
import com.zlzkj.app.model.Role;
import com.zlzkj.app.model.Shop;
import com.zlzkj.app.service.AdminService;
import com.zlzkj.app.service.ShopService;
import com.zlzkj.app.util.UIUtils;
import com.zlzkj.core.base.BaseController;
import com.zlzkj.core.sql.Row;

@Controller
@RequestMapping(value = { "shop" })
public class ShopController extends BaseController {

	@Autowired
	private ShopService shopService;

	@RequestMapping(value = { "edit" })
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {

		Admin admin = (Admin) request.getSession().getAttribute("admin");

		System.out.println("admin.getId()：" + admin.getId());
		List<Row> shopList = shopService.findShopById(admin.getId());

		model.addAttribute("shopList", shopList);
		if (request.getMethod().equals("POST")) {
			return ajaxReturn(response, shopList);
		} else {
			return "shop/shop";
		}
	}

	// @RequestMapping(value={"shop_add"})
	// public String add(Model model, HttpServletRequest request,
	// HttpServletResponse response) {
	//
	// Admin admin= (Admin) request.getSession().getAttribute("admin");
	//
	// System.out.println("myShop_id:"+admin.getId());
	//
	// List<Row> shopList =shopService.findShopById(admin.getId());
	//
	// model.addAttribute("shopList",shopList);
	// if(request.getMethod().equals("POST")){
	// return ajaxReturn(response, shopList);
	// }else{
	// return "shop/shop_add";
	// }
	// }

	@RequestMapping(value = { "shop_add" })
	public String roleAdd(Model model, HttpServletRequest request,
			HttpServletResponse response, Shop shop) throws Exception {
		if (request.getMethod().equals("POST")) {
			System.out.println(shop.getShopName() + "   " + shop.getAddress()
					+ "  " + shop.getPhone() + "  " + shop.getWechat() + "  "
					+ shop.getStatus());
			Date timeDate = new Date();
			int addTime = (int) (timeDate.getTime() / 1000);
			shop.setAddTime(addTime);
			int flag = shopService.insertShop(shop);
			if (flag == 1) {
				return ajaxReturn(response, null, "添加成功", 1);
			} else {
				return ajaxReturn(response, null, "发生错误，请重试！", 0);
			}
		}
		return "shop/shop_add";
	}

	@RequestMapping(value = { "shop_edit" })
	public String roleEdit(Model model, HttpServletRequest request,
			HttpServletResponse response, Shop shop) {
		try {
			System.out.println("1");

			if (shop.getId() != null) {
				model.addAttribute("shop", shopService.findById(shop.getId()));
			}

			System.out.println("1");
			if (request.getMethod().equals("POST")) {
				int flag;
				flag = shopService.update(shop);
				if (flag == 1) {
					return ajaxReturn(response, null, "添加成功", 1);
				} else {
					return ajaxReturn(response, null, "发生错误，请重试！", 0);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("错了");
		}
		return "shop/shop_edit";
	}

	@RequestMapping(value = { "shop_delete" })
	public String roleDel(HttpServletRequest request,
			HttpServletResponse response, Integer id) {
		// if(request.getMethod().equals("POST")){
		System.out.println(id);
		shopService.delete(id);
		return ajaxReturn(response, null, "删除成功", 1);
	}

}
